package com.bysj.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.bysj.dao.BbsDao;
import com.bysj.entity.Bbs;
import com.bysj.util.Dbutils;

public class BbsDaoImpl implements BbsDao{
	private Dbutils db=new Dbutils();
	@Override
	public void addBBS(String sql, Bbs b) {
		Connection conn=db.getConnection();
		PreparedStatement  pst=null;
		try {
			pst=conn.prepareStatement(sql);
			////
			pst.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.closeAll(null, pst, conn);
		}
	}

	@Override
	public void delBBS(String sql, Bbs b) {
		Connection conn=db.getConnection();
		PreparedStatement pst=null;
		try {
			pst=conn.prepareStatement(sql);
			pst.setInt(1, b.getId());
			pst.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.closeAll(null, pst, conn);
		}
	}

	@Override
	public int AllBBS(String sql) {
		Connection conn=db.getConnection();
		PreparedStatement pst=null;
		ResultSet set=null;
		int count=0;
		try {
			pst=conn.prepareStatement(sql);
			set=pst.executeQuery();
			if(set.next()){
				count=set.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.closeAll(set, pst, conn);
		}
		return count;
	}

	@Override
	public ArrayList<Bbs> queryAllBBS(String sql) {
		ArrayList<Bbs> list=new ArrayList<Bbs>();
		Connection conn=db.getConnection();
		PreparedStatement pst=null;
		ResultSet  set=null;
		Bbs bbs=null;
		try {
			pst=conn.prepareStatement(sql);
			set=pst.executeQuery();
			while(set.next()){
				bbs=new Bbs();
				bbs.setId(set.getInt(1));
				bbs.setUname(set.getString(2));
				bbs.setMood(set.getString(3));
				bbs.setContent(set.getString(4));
				bbs.setTime(set.getDate(5));
				list.add(bbs);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.closeAll(set, pst, conn);
		}
		return list;
	}

	@Override
	public ArrayList<Bbs> FenyequeryAllBBS(String sql, int page, int rows) {
		ArrayList<Bbs> list=new ArrayList<Bbs>();
		Connection conn=db.getConnection();
		PreparedStatement pst=null;
		ResultSet  set=null;
		Bbs bbs=null;
		try {
			pst=conn.prepareStatement(sql);
			pst.setInt(1, (page-1)*rows);
			pst.setInt(2, rows);
			set=pst.executeQuery();
			while(set.next()){
				bbs=new Bbs();
				bbs.setId(set.getInt(1));
				bbs.setUname(set.getString(2));
				bbs.setMood(set.getString(3));
				bbs.setContent(set.getString(4));
				bbs.setTime(set.getDate(5));
				list.add(bbs);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.closeAll(set, pst, conn);
		}
		return list;
	}

	@Override
	public Bbs queryBBs(String sql, Bbs bbs) {
		// TODO Auto-generated method stub
		return null;
	}

}
